import json
import os
# from win10toast import ToastNotifier
from urllib import request, parse

base_path = os.path.join(os.getcwd().split('StockMonitor')[0] + 'StockMonitor')


# 云之讯 短信提示
def yzx_msg(title, msg, color):
    # 指定模板单发
    url = 'https://open.ucpaas.com/ol/sms/sendsms'
    params = {
        "sid": "e89937b2f2e9e421a409874b12411280",
        "token": "8e649cc721c**********cef2378f13d",
        "appid": "7ea9f9cc208346709a66fc45035ebb9a",
        "templateid": "154501",
        "param": "87828,3",
        "mobile": "18011984299",
        "uid": "2d92c6132139467b989d087c84a365d8"
    }
    req_url = 'https://open.ucpaas.com/ol/sms/{function}'


# # win 10 右下标提示
# def win_msg(title, msg, color):
#     # 有icon的版本
#     ico_path = os.path.join(base_path + '\\utils\\pic\\' + color + '.ico')
#     # 间隔提示
#     count = 0
#     while 1:
#         toaster = ToastNotifier()
#         toaster.show_toast(title, msg, icon_path=ico_path, duration=10)
#         # 等待提示框关闭
#         while toaster.notification_active(): time.sleep(0.1)
#         count += 1
#         if count >= 1:
#             break


# miao_code = "t1qznXH"
def miao_msg(code, text):
    if len(text) == 0:
        return 0
    if len(text) > 2000:
        return 0
    print(text)
    page = request.urlopen(
        "http://miaotixing.com/trigger?" + parse.urlencode({"id": code, "text": text, "type": "json"}))
    result = page.read()
    jsonObj = json.loads(result)
    if (jsonObj["code"] == 0):
        print("成功")
        return 1
    else:
        print("失败，错误代码：" + str(jsonObj["code"]) + "，描述：" + jsonObj["msg"])
        return 0


def sys_miao_msg(text):
    return miao_msg('tOCarr1', text)


if __name__ == '__main__':
    print()
    # win_msg("aaaaa", 'tupwrjx', color='r')
    msg = "{'code': '002622', 'name': '融钰集团', 'exchange': '0'}"
    miao_msg("t1qznXH", msg)
